﻿using CommonClass;
using EnvControlLib;
using SurveillanceLib;
using SurveillancePro.Base.Events;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;

namespace EnvCommonUtility
{
    /// <summary>
    /// 用于回放视频的分屏
    /// </summary>
    public class EnvPanelOperationExt
    {
        #region 属性

        private static EnvVideoPanel _selectedPanel;
        /// <summary>
        /// 记录所选 Panel，只能选择一个 Panel【Static】
        /// </summary>
        public static EnvVideoPanel SelectedPanel
        {
            get { return _selectedPanel; }
            set
            {
                if (_selectedPanel == value) return;
                _selectedPanel = value;

                try
                {
                    if (EnvUtilities.EnvEventAggregator != null)
                    {
                        // 修改播放器按钮状态
                        if (value != null && value.Tag != null && value.Tag is TagModel)
                        {
                            EnvUtilities.EnvEventAggregator.GetEvent<OpenModelEvent>().Publish(new OpenModelEventArg() { ModelName = "ViewModel_PlayVideo", FunctionName = "ChangePlayButtonState", Arg = null, }); // 事件通知
                        }
                    }
                }
                catch (System.Exception ex)
                {
#if DEBUG
                    Debug.WriteLine(ex.Message);
#endif
                }
            }
        }

        #endregion

        #region 方法

        /// <summary>
        /// 将 SelectedPanel 设为 null
        /// </summary>
        public void SetNull2SelectedPanel()
        {
            SelectedPanel = null;
        }

        /// <summary>
        /// 分屏
        /// </summary>
        /// <param name="screenCount">分屏个数</param>
        /// <param name="splitScreen">基分屏</param>
        /// <param name="maxScreen">最大化窗口</param>
        /// <param name="doubleAction">双击事件</param>
        /// <param name="showMaxScreenAction">最大化视频窗口事件</param>
        /// <param name="hideMaxScreenAction">最小化视频窗口事件</param>
        /// <param name="stopPanelPlayBackAction">停止指定窗口中的视频事件</param>
        /// <returns>所有分割的窗口</returns>
        public List<EnvVideoPanel> SplitScreen(int screenCount, System.Windows.Controls.Panel splitScreen,
            EnvVideoPanel maxScreen, Action doubleAction = null,
            Action<EnvVideoPanel, System.Windows.Controls.Panel, EnvVideoPanel, Action> showMaxScreenAction = null,
            Action<System.Windows.Controls.Panel, EnvVideoPanel, Action> hideMaxScreenAction = null,
            Action<EnvVideoPanel> stopPanelPlayBackAction = null)
        {
            try
            {
                SplitScreen splitscreen = new SplitScreen(); // 画面分割
                List<EnvVideoPanel> panelList = new List<EnvVideoPanel>(); // 保存分屏列表
                Size size = new Size()
                {
                    Height = (int)splitScreen.ActualHeight,
                    Width = (int)splitScreen.ActualWidth,
                };
                IList<Rectangle> rectList = splitscreen.CalcPanelRectangle(screenCount, size);

                splitScreen.Children.Clear(); // 先清除 Panel 已存在的内容
                for (int i = 0; i < rectList.Count; i++)
                {
                    EnvVideoPanel panel = new EnvVideoPanel()
                    {
                        Width = rectList[i].Width,
                        Height = rectList[i].Height,
                        Tag = new TagModel()
                        {
                            Panel_Index = i,
                        },
                    };

                    if (panel.DataContext != null &&
                        panel.DataContext is ViewModel_EnvVideoPanel)
                    {
                        ViewModel_EnvVideoPanel vm_EnvVideoPanel = panel.DataContext as ViewModel_EnvVideoPanel;
                        vm_EnvVideoPanel.IsMaxPanel = false;
                        vm_EnvVideoPanel.IsRealPlayPanel = System.Windows.Visibility.Collapsed;
                        vm_EnvVideoPanel.PanelCurrentList = panelList;
                        vm_EnvVideoPanel.SplitScreen = splitScreen;
                        vm_EnvVideoPanel.MaxScreen = maxScreen;
                        vm_EnvVideoPanel.DoubleAction = doubleAction;
                        vm_EnvVideoPanel.ChangeBorderColorAction = EnvUtilities.ChangeBorderColor4Preview;
                        vm_EnvVideoPanel.ShowMaxScreenAction = showMaxScreenAction ?? EnvUtilities.ShowMaxScreen;
                        vm_EnvVideoPanel.HideMaxScreenAction = hideMaxScreenAction ?? EnvUtilities.HideMaxScreen;
                        vm_EnvVideoPanel.StopPanelRealPlayAction = stopPanelPlayBackAction ?? EnvUtilities.StopPanelRealPlay;
                    }

                    splitScreen.Children.Add(panel);
                    panelList.Add(panel);
                }

                return panelList;
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
#endif
                return null;
            }
        }

        #endregion
    }
}
